package com.kaiyidiannao.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandHouse;
import com.kaiyidiannao.model.req.model.GetCloseOpen;
import com.kaiyidiannao.view.ScaleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 2014/4/16.
 */
public class MySecondHandHouseAdapter extends ImageLoaderBaseAdapter {

    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<SecondHandHouse> list = new ArrayList<SecondHandHouse>();
    private String[] imgs;
    private Context context;
    private int tip = 0;

    public MySecondHandHouseAdapter(Context context, List<SecondHandHouse> list,int tip) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.tip = tip;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView mySecondHandHouseItemNameText = null;
        TextView mySecondHandHouseItemPriceText = null;
        TextView mySecondHandHouseItemAreaText = null;
        TextView publishTime;
        ImageButton mySecondHandHouseItemButton = null;
        ScaleImageView imageView = null;
        TextView phoneNum = null;
        LinearLayout call = null;
        final SecondHandHouse secondHandHouse = list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.mysecondhand_house_item, null);
        }
        imageView = (ScaleImageView) convertView.findViewById(R.id.SecondHandHouse_item_ImageView);
        mySecondHandHouseItemNameText = (TextView) convertView.findViewById(R.id.mySecondHandHouseItemNameText);
        mySecondHandHouseItemPriceText = (TextView) convertView.findViewById(R.id.mySecondHandHouseItemPriceText);
        mySecondHandHouseItemAreaText = (TextView) convertView.findViewById(R.id.mySecondHandHouseItemAreaText);
        mySecondHandHouseItemButton = (ImageButton) convertView.findViewById(R.id.mySecondHandHouseItemButton);
        publishTime = (TextView)convertView.findViewById(R.id.publishTime);
        call = (LinearLayout)convertView.findViewById(R.id.call);
        phoneNum = (TextView) convertView.findViewById(R.id.phoneNum);
//        imgs = (secondHandHouse.getThumb() + ",").split(",");
//        imageLoader.displayImage(imgs[0], imageView);
        imageLoader.displayImage(secondHandHouse.getThumb(),imageView, options, null);
        mySecondHandHouseItemNameText.setText(secondHandHouse.getTitle());
        if (secondHandHouse.getPrice() == null) {
            mySecondHandHouseItemPriceText.setText("暂未提供");
        } else {
            mySecondHandHouseItemPriceText.setText(secondHandHouse.getPrice() + "万");
        }
        if (secondHandHouse.getArea() == null) {
            mySecondHandHouseItemAreaText.setText("暂未提供");
        } else {
            mySecondHandHouseItemAreaText.setText(secondHandHouse.getArea() + "㎡");
        }
        phoneNum.setText(secondHandHouse.getTelephone());

        publishTime.setText(secondHandHouse.getAddtime());

        if(list.get(position).getStatus()!=null){
            if (list.get(position).getStatus().equals("2")) {
                mySecondHandHouseItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_dark_button));
            }else {
                mySecondHandHouseItemButton.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_light_button));
            }

        }
        if (null != convertView) {
            if (null != mySecondHandHouseItemButton) {

                mySecondHandHouseItemButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DataCenter.position = position;
                        ((BaseActionBarActivity)context).showProgressDialog();
                        if (Integer.parseInt(list.get(position).getStatus()) == 2) {
                            new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "open", DataCenter.beansHouse.get(position).getItemid(), "28", DataCenter.secondHandHouse_category_id + "").execute();
                        } else if (Integer.parseInt(list.get(position).getStatus()) == 3) {
                            new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "close", DataCenter.beansHouse.get(position).getItemid(), "28", DataCenter.secondHandHouse_category_id + "").execute();
                        }
                    }
                });
            }

            if (null != call){
                call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String phoneNum =  secondHandHouse.getTelephone();
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + phoneNum));
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }
                });
            }
        }



        return convertView;
    }
}
